package com.yzkj.cpjxc.business.service.impl;

import com.yzkj.cpjxc.business.framework.common.BusinessMap;
import com.yzkj.cpjxc.business.framework.common.Pager;
import com.yzkj.cpjxc.business.domain.InitialFinancialBalance;
import com.yzkj.cpjxc.business.domain.InitialFinancialBalanceQueryVo;
import com.yzkj.cpjxc.business.service.InitialFinancialBalanceService;
import org.apache.ibatis.session.RowBounds;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 财务初始余额表 Service 实现
 */
@Service
public class InitialFinancialBalanceServiceImpl implements InitialFinancialBalanceService {

	/**
	 * MyBatis 命名空间
	 */
	private final static String MAPPER_NAMESPACE = "com.chkj.xezz.initialFinancialBalance.mappers.InitialFinancialBalanceMapper";

	@Autowired
	private SqlSessionTemplate sqlSessionTemplate;
	
	@Override
	public BusinessMap create(InitialFinancialBalance initialFinancialBalance) throws Exception {
		BusinessMap bm = new BusinessMap();
		//if (this.isExists(initialFinancialBalance)) {
		//	bm.put(Constants.WARNING_MSG, "该财务初始余额表名称已经存在");
		//	return bm;
		//}
		sqlSessionTemplate.insert(MAPPER_NAMESPACE + ".add", initialFinancialBalance);
		return bm;
	}

	@Override
	public boolean isExists(InitialFinancialBalance initialFinancialBalance) throws Exception {
		return (Integer)sqlSessionTemplate.selectOne(MAPPER_NAMESPACE + ".isExists", initialFinancialBalance) > 0;
	}
	
	@Override
	public InitialFinancialBalance getById(String uuid) throws Exception {
		return sqlSessionTemplate.selectOne(MAPPER_NAMESPACE + ".getById", uuid);
	}
	
	@Override
	public BusinessMap edit(InitialFinancialBalance initialFinancialBalance) throws Exception {
		BusinessMap bm = new BusinessMap();
		//if (this.isExists(initialFinancialBalance)) {
		//	bm.put(Constants.WARNING_MSG, "该财务初始余额表名称已经存在");
		//	return bm;
		//}
		sqlSessionTemplate.update(MAPPER_NAMESPACE + ".edit", initialFinancialBalance);
		return bm;
	}

	@Override
	public boolean delete(String uuid) throws Exception {
		sqlSessionTemplate.delete(MAPPER_NAMESPACE + ".deleteById", uuid);
		return true;
	}

	@Override
	public Pager<InitialFinancialBalance> search(Pager<InitialFinancialBalance> pager, InitialFinancialBalanceQueryVo queryVo) throws Exception {
		List<InitialFinancialBalance> dataList = sqlSessionTemplate.selectList(MAPPER_NAMESPACE + ".page", queryVo, new RowBounds((pager.getCurrentPage() - 1) * pager.getLimit(), pager.getLimit()));
		pager.setDataList(dataList);
		
		// 如果需要转换成ShowVo展示，那么就在这里转换
		
		Long total = sqlSessionTemplate.selectOne(MAPPER_NAMESPACE + ".getTotal", queryVo);
		pager.setTotal(total);
		return pager;
	}

	@Override
	public InitialFinancialBalance getByZhangTaoIdAndKeMuId(String zhangTaoId, String keMuId) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("zhangTaoId", zhangTaoId);
		map.put("keMuId", keMuId);
		return sqlSessionTemplate.selectOne(MAPPER_NAMESPACE + ".getByZhangTaoIdAndKeMuId", map);
	}

}
